package cn.itcast.web.handler;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ControllerAdvice  //声明它是一个异常处理器
public class MyExceptionHandler implements HandlerExceptionResolver {

    @ExceptionHandler //若没有value属性的话 指处理所有的异常.
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView mv = new ModelAndView();

        /*处理权限不足的异常*/
        if (ex instanceof UnauthorizedException) {
            mv.setViewName("redirect:/unauthorized.jsp");
            return mv;
        }

        mv.setViewName("error");
        return mv;
    }
}
